#!/usr/bin/env bash

. "test/testenv.sh"
set -e

SHUTDOWN_LFS=no
SHOW_LOGS=yes

atexit() {
  res=${1:-$?}
  SHUTDOWN_LFS=yes
  if [ "$res" = "0" ]; then
    SHOW_LOGS=no
  fi

  if [ "$SHOW_LOGS" = "yes" ] && [ "$VERBOSE_LOGS" = "1" ]; then
    if [ -s "$REMOTEDIR/gitserver.log" ]; then
      echo ""
      echo "gitserver.log:"
      cat "$REMOTEDIR/gitserver.log"
    fi

    echo ""
    echo "env:"
    env
  fi

  shutdown
  exit $res
}

trap "atexit" EXIT

if [ -s "$LFS_URL_FILE" ]; then
  SHOW_LOGS=no
  echo "$LFS_URL_FILE still exists!"
  echo "Confirm other tests are done, and run:"
  echo "  $ curl $(cat "$LFS_URL_FILE")/shutdown"
  exit 1
fi

setup

parallel=${GIT_LFS_TEST_MAXPROCS:-4}

echo "Running this maxprocs=$parallel"
echo

GO15VENDOREXPERIMENT=0 GIT_LFS_TEST_MAXPROCS=$parallel GIT_LFS_TEST_DIR="$GIT_LFS_TEST_DIR" SHUTDOWN_LFS="no" go run script/*.go -cmd integration "$@"
